<?php
/*
Plugin Name: Nmedia File Uploader Plugin
Plugin URI: http://www.najeebmedia.com
Description: This Plugin is developed by NajeebMedia.com
Version: 1.1
Author: Najeeb Ahmad
Author URI: http://www.najeebmedia.com/

*/



/*ini_set('display_errors',1);
error_reporting(E_ALL);*/




class nmFileUploader {
	
	var $fileupload_db_version = "1.0";
	
	/*
	data vars
	*/
	
	static $name;
	static $file_name;
	static $notes;
	
	
	
	
	/**
	* constructor
	*/	 
	function nmFileUploader() {
		
		//$this -> loadJS();
	}
	
	
	function test()
	{
		echo 'yes man';
	}
	
	function renderUserArea()
	{
		global $wpdb ;
		global $user_ID;
		global $current_user;
		get_currentuserinfo();
		
		
		if ( is_user_logged_in() ) { 
			nmFileUploader::renderForm();
		}
		else
		{
		
			/*wp_redirect( home_url() ); exit;*/
			echo '<script type="text/javascript">
			window.location = "'.wp_login_url( get_permalink() ).'"
			</script>';
		}
		
	}
	
	
	
	
	public function fileuploader_install() {
		global $wpdb;
		global $fileupload_db_version;
	
		$table_name = $wpdb->prefix . "userfiles";
		  
		$sql = "CREATE TABLE `$table_name` 
				(`fileID` INT( 9 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
				`userID` INT( 7 ) NOT NULL ,
				`fileName` VARCHAR( 250 ) NOT NULL ,
				`fileDescription` MEDIUMTEXT NOT NULL ,
				`fileType` VARCHAR( 15 ) NOT NULL ,
				`fileUploadedOn` DATETIME NOT NULL );";
	
	   require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
	   dbDelta($sql);
	 
	   add_option("fileupload_db_version", $fileupload_db_version);
	}


	/*
	form uploader
	*/
	public function renderForm()
	{
		$file = dirname(__FILE__).'/upload-form.php';
		include($file);
	}
	
	
		
	
  
  public function set_up_admin_page () {
	
	add_menu_page(	'FileUploader', 
					'FileUploader Plugin', 
					'manage_options', 
					'realtor-settings', 
					array('nmFileUploader', 'filupload_admin_options'),
					'',
					4);
	
	/*add_submenu_page( 'realtor-settings',
					  'Property Filess', 
					  'Property Filess', 
					  'manage_options', 
					  'property-listings', 
					  array('nmFileUploader', 'show_listings'));*/
  	
	}
	
	
	public function filupload_admin_options()
	{
		$file = dirname(__FILE__).'/options.php';
		include($file);
	}
	
	/*public function show_listings()
	{
		$file = dirname(__FILE__).'/listings.php';
		include($file);
	}*/
	
 
  
  
  
	public function saveFile()
	{
		global $user_ID;
		global $wpdb;
		
		$dt = array(	'name'		=> nmFileUploader::$name,
						'file'		=> nmFileUploader::$file_name,
						'notes'		=> nmFileUploader::$notes,
						'uid'		=> $user_ID,
						'date'		=> current_time('mysql')
					 );
					 
					 
		//var_dump($dt);
		
		$wpdb -> insert($wpdb->prefix . "dlg_cu",
						$dt		
						);
		
		if($wpdb->insert_id)
			return true;
		else
			return false;
						
		//$wpdb->print_error(); 
	}
	
	
	/*
	deleting file
	*/
	public function deleteFile($fileid)
	{
		global $wpdb;
		$res = $wpdb->query(
						"
						DELETE FROM ".$wpdb->prefix . "dlg_cu
						WHERE id = $fileid"
					);
		return $res;
	}
	
	
	/*
	Get Files Detail
	*/
	
	function getUserFiles()
	{
		//echo "hello";
		global $wpdb;
		global $user_ID;
		
		$myrows = $wpdb->get_results( "SELECT * FROM wp_dlg_cu
						  			   where uid = $user_ID
									   ORDER BY date DESC");
	   return $myrows;
	}
	
	
	/*
	action hook function
	action hook: edit user
	*/
	
	function nm_user_upload_admin(){
	
	global $wpdb;
	
	$user_id = $_REQUEST['user_id'];
	
	if($_GET['dlg-delete-file'] != ""){
		$wpdb->query("
					DELETE FROM ".$wpdb->prefix."dlg_cu WHERE id = ".$_GET['dlg-delete-file']."
					");
	
	}
	
	
	echo '<h2>User Uploads</h2><a name="downloads"></a>';
	
		
	$r = $wpdb->get_results("SELECT *  FROM ".$wpdb->prefix."dlg_cu   where uid = $user_id  order by date desc", ARRAY_A);
	if($r == FALSE){
		$html .=  '<p style="color:red">This user does not have any uploads!</p>';	
	}else{
		//show uploaded documents
		  $html .= '
					<script type="text/javascript">
					function dlg_client_upload_email_vendor(){
	
					jQuery.ajax({
						 
					  type: "POST",
					  url:  "../wp-content/plugins/dlg-client-upload/ajax.php?function=email-vendor" ,
					 
					 data:  jQuery("#your-profile" ).serialize(),
					  success: function(msg){
											jQuery("#updateme").empty();
											jQuery("#updateme").append( msg);
											
										  }
					});	
				
				return false;
			}
			
			function confirmFirst(url)
			{
				var a = confirm("Are you sure you want to delete this file?");
				if(a)
				{
					window.location = url;
				}
			}
			</script>
			  <table class="wp-list-table widefat fixed posts" cellspacing="0">
				<thead>
				<tr>
			<th>File Name</th>
			<th>Date</th>
			<th>Download</th>
			<!--<th>Email</th>-->
			</tr>
				</thead>
			';
				for($i=0; $i<count($r); $i++){
				$fileName = ABSPATH. 'wp-content/plugins/plugin-fileuploader/uploads/'.	$r[$i]['file'];	
				
				if(file_exists($fileName))		// if file exisits
				{
					$urlDwnld = get_option('uploader_page_url') . '/?dwnld='.$r[$i]['file'];
					
					if($r[$i]['name'] == ""){
						
						$name = $r[$i]['file'];
					}else{
						
						$name = $r[$i]['name'];
					}
				$html .= '
	
					 <tr>
						<td ><strong>'.stripslashes($name).'</strong>
						<br><em>Notes: '.$r[$i]['notes'].'</em>
						
						</td>
						 <td >'.date('F jS Y h:i A', strtotime($r[$i]['date'])).'</td>
					   
						<td><a style="margin-right:15px" href="' . $urlDwnld.'" target="blank">Download</a> <a href="javascript:confirmFirst(\'user-edit.php?user_id='.$_GET['user_id'].'&dlg-delete-file='.$r[$i]['id'].'\')">Delete</a> </td>
					
					<!--<td><input type="checkbox" name="vendor_email[]" value="'.$r[$i]['id'].'"></td>	-->
					
					</tr>
					  
					  ';	
					
				}
				
			/*$html .= '</table>
			
				<div style="text-align:right">
			<div id="updateme"></div>
				Choose  the files you want to send above, type a message and choose a vendor then click submit:  <select name="vendor">
				';
				
				
				
				if($_POST['submit-vendor'] != ""){
					
				//	print_r($_POST);
					
					
				}
				
			$vendors = $wpdb->get_results("SELECT *  FROM ".$wpdb->prefix."options   where option_name  LIKE 'dlg_client_upload_vendors%'  order by option_name", ARRAY_A);
						for($i=0; $i<count(	$vendors); $i++){
				
				$vendor_info[$i] = unserialize($vendors[$i]['option_value']);
				
				$html .=  '<option value="'.$vendor_info[$i]['email'].'">'.$vendor_info[$i]['name'].'</option>';
				
						}
				
				
				$html .= '</select> Message: <input type="text" name="vendor-message"> <select name="vendor_attach"><option value="1">Attach to email</option><option value="0">Send links to files</option><option value="3">Attach and link to to files</option></select> <input type="submit" name="submit-vendor" value="Email vendor files!" onclick="dlg_client_upload_email_vendor();return false;"> 
				</div>
				';*/
				
		
	}
	echo $html;
	
	}
	
	}	// if file exisits
  }
  
  /*
	action hook function
	action hook: edit user
	*/

add_action('admin_menu', array('nmFileUploader', 'set_up_admin_page'));

register_activation_hook(__FILE__, array('nmFileUploader','fileuploader_install'));



function load_fileuploader_script() {
	/*wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', plugins_url('js/jquery-1.4.4.min.js', __FILE__));
	wp_enqueue_script( 'jquery' );*/
	
	/*wp_register_script('file_uploader_script1', plugins_url('js/uploader/fileuploader.js', __FILE__));
	wp_enqueue_script('file_uploader_script1');*/
	
	wp_register_script('swfobject_script', plugins_url('js/uploadify/swfobject.js', __FILE__));
	wp_enqueue_script('swfobject_script');
	
	wp_register_script('uploadify_script', plugins_url('js/uploadify/jquery.uploadify.v2.1.4.min.js', __FILE__));
	wp_enqueue_script('uploadify_script');
	
	
     wp_register_script( 'fileuploader_custom_script', plugins_url('js/fileuploader_custom.js', __FILE__));
	 wp_enqueue_script('fileuploader_custom_script');
	 
	 //fancy box
}    

add_action('wp_enqueue_scripts', 'load_fileuploader_script');

//edit user action
add_action( 'edit_user_profile', array('nmFileUploader', 'nm_user_upload_admin'));


add_shortcode( 'nm-wp-file-uploader', array('nmFileUploader', 'renderUserArea'));
//var_dump($nm_sticky);
//$e -> saveFiles();



add_action('wp_print_styles', 'nm_fileuploader_style');

    /*
     * Enqueue style-file, if it exists.
     */

    function nm_fileuploader_style() {
		/*wp_register_style('fileuploader_stylesheet', plugins_url('js/uploader/fileuploader.css', __FILE__));
        wp_enqueue_style( 'fileuploader_stylesheet');*/
		
		//uploadify css
		wp_register_style('fileuploader_stylesheet', plugins_url('js/uploadify/uploadify.css', __FILE__));
        wp_enqueue_style( 'fileuploader_stylesheet');
		
		wp_register_style('plugin_fileuploader_stylesheet', plugins_url('nm_fileuploader_style.css', __FILE__));
        wp_enqueue_style( 'plugin_fileuploader_stylesheet');
        
    }


?>